//
//  SFPAddCheckAppointmentController.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/9.
//

import UIKit

import ProgressHUD
import BRPickerView
class SFPAddCheckAppointmentController: SFPBaseViewController {
    @IBOutlet var nameTF: UITextField!
    @IBOutlet var locationTF: UITextField!
    @IBOutlet var doctorTF: UITextField!
    @IBOutlet var timeTD: UITextField!
    @IBOutlet var dateTF: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func gotoBack(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    @IBAction func gotoSave(_ sender: Any) {
        self.view.endEditing(true)
        if self.timeTD.text!.isEmpty {
            showTips("Please select appointment time")
            return
        }
        if self.dateTF.text!.isEmpty {
            showTips("Please select appointment date")
            return
        }
        if self.nameTF.text!.isEmpty {
            showTips("Please enter booking content")
            return
        }
        if self.locationTF.text!.isEmpty {
            showTips("Please enter location")
            return
        }
        if self.doctorTF.text!.isEmpty {
            showTips("Please enter booking person")
            return
        }
        let model = SFPCheckAppointmentModel()
        model.name = self.nameTF.text!
        model.time = self.timeTD.text!
        model.date = self.dateTF.text!
        model.location = self.locationTF.text!
        model.doctor = self.doctorTF.text!
        model.completed = false
        model.isDefault = false
        model.saveToDB()
        let helper = SFPCheckAppointmentModel.getUsingLKDBHelper()
        helper.delete(with: SFPCheckAppointmentModel.self, where: "isDefault = true")
        ProgressHUD.showSuccess("More record addition succeed")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popToRootViewController(animated: true)
        }
    }
    @IBAction func gotoSelectAppointment(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRDatePickerView(pickerMode: .HM)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.resultBlock = { selectDate,selectValue in
            self.timeTD.text = String(format: "%@", selectValue!)
        }
        pickerView.show()
    }
    @IBAction func gotoSelectDateAction(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRDatePickerView(pickerMode: .YMD)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.resultBlock = { selectDate,selectValue in
            self.dateTF.text = selectDate?.toString(format: "yyyy.MM.dd")
        }
        pickerView.show()
    }
    
}
